package org.FaPe3D.engine.elements
{
	import flash.utils.Dictionary;
	
	import org.FaPe3D.engine.materials.*;
	import org.FaPe3D.utils.InitialValues;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.FaPe3D.utils.MouseOverManager;
	import org.papervision3d.events.InteractiveScene3DEvent;
	
	public class FPMagicalCube extends DisplayObject3D
	{
		public var front:FPPlane;
		public var back:FPPlane;
		public var left:FPPlane;
		public var right:FPPlane;
		public var top:FPPlane;
		public var bottom:FPPlane;
		public var sides:Dictionary = new Dictionary(true);
		private var _width:Number;
		private var _height:Number;
		private var _depth:Number;
		private var _magical:Boolean = true;
		private var _hand;
		
		public function FPMagicalCube()
		{
			_prepareFaces();
			_setPositions();
			_prepareDictionary();
			_width = InitialValues.MinWidth;
			_height = InitialValues.MinHeight;
			_depth = InitialValues.MinDepth;
			super.name = 'Magical';
			super.addChild(_cube());
		}
		
		private function _prepareFaces():void
		{
			front = new FPPlane();
			back = new FPPlane();
			left = new FPPlane();
			right = new FPPlane();
			top = new FPPlane();
			bottom = new FPPlane();
		}
		
		private function _setPositions():void
		{
			front.x = left.width/2;
			front.z = left.width/2;
			front.rotationY = 90;
			back.x = -left.width/2;
			back.z = left.width/2;
			back.rotationY = 90;
			left.z = left.width /2;
			left.y = left.width /2;
			left.rotationY = 90;
			left.rotationX = 90;
			right.z = left.width /2;
			right.y = -left.width /2;
			right.rotationY = 90;
			right.rotationX = 90;
			top.z = left.height;			
		}
		
		public function getSide(name:String):FPPlane
		{
			return sides[name];
		}
		
		public function getSideMaterial(name:String):MaterialObject3D
		{
			return sides[name].material;
		}
		
		private function _prepareDictionary():void
		{
			sides['front'] = front;
			sides['back'] = back;
			sides['left'] = left;
			sides['right'] = right;
			sides['top'] = top;
			sides['bottom'] = bottom;
		}
		
		private function _cube():DisplayObject3D
		{
			var container:DisplayObject3D = new DisplayObject3D();
			for (var i in sides)
			{
				container.addChild(sides[i],i);
				sides[i].material.doubleSided = true;
			}
			return container;
		}
		
		
		public function get useHandCursor():Boolean
		{
			return _hand;
		}
		
		public function set useHandCursor(action:Boolean):void
		{
			_hand = action;
			if(_hand == true)
			{
				this.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,MouseOverManager.rollOver);
				this.addEventListener(InteractiveScene3DEvent.OBJECT_OUT,MouseOverManager.rollOut);
			}
			if(_hand == false)
			{
				if(this.hasEventListener(InteractiveScene3DEvent.OBJECT_OVER))
				{
					this.removeEventListener(InteractiveScene3DEvent.OBJECT_OVER,MouseOverManager.rollOver);
					this.removeEventListener(InteractiveScene3DEvent.OBJECT_OUT,MouseOverManager.rollOut);
				}
			}
		}

		public function get width():Number
		{
			return _width;
		}
		
		public function set width(num:Number):void
		{
			this.scaleX = num /_width;
			_width = num;
		}
		
		public function get height():Number
		{
			return _height;
		}
		
		public function set height(num:Number):void
		{
			this.scaleY = num /_height;
			_height = num;
		}
		
		public function get depth():Number
		{
			return _depth;
		}
		
		public function set depth(num:Number):void
		{
			this.scaleZ = num /_depth;
			_depth = num;
		}
		
		public function get magical():Boolean
		{
			return _magical;
		}
		
		
	}
}